package scripts;

import api.EmpApi;
import api.LoginApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import util.HttpClientUtil;
import util.ResponseData;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class TestLogin {
    private  final LoginApi loginApi=new LoginApi();
    private  final EmpApi empapi=new EmpApi();
    private String emp_id=null;
    private static Logger logger= LoggerFactory.getLogger(TestLogin.class);
    @DataProvider
    public Object[][] buildLogin_data() throws IOException {
        String file = FileUtils.readFileToString(new File("./data/login.json"), "utf-8");
        JSONArray jsonArray=JSONObject.parseArray(file);
        Object[][] test_data=new Object[jsonArray.size()][5];
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);
            String mobile = jsonObject.getString("mobile");
            String password = jsonObject.getString("password");
            String code = jsonObject.getString("code");
            String success = jsonObject.getString("success");
            String message = jsonObject.getString("message");
            test_data[i]=new Object[]{mobile,password,code,success,message};
        }
        return test_data;
    }
    @Test(dataProvider = "buildLogin_data")
    public static void test_01(String mobile,String password,String code,String success,String message) throws IOException, URISyntaxException {
        ResponseData responseData = LoginApi.login(mobile, password);
        logger.debug("responseData={}",responseData);
        String token= responseData.getBody().getString("data");
        HttpClientUtil.build_header("Authorization","Bearer "+token);
    }

}
